package com.david.viewtest.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View

class RectBannerIndicator(
    context: Context,
    attrs: AttributeSet,
) : View(context, attrs) {
    private var width = 0f
    private var height = 0f
    private var darkWidth = 0f


    var bannerCount = 5
        set(value) {
            field = value
            postInvalidate()
        }

    var bannerIndex = 1
        set(value) {
            field = value
            postInvalidate()
        }

    private var normalColor = Color.parseColor("#E6E6E6")
    private var selectColor = Color.parseColor("#24292B")

    private var paint = Paint()

    //init {
    //    setOnClickListener {
    //        bannerIndex = (bannerIndex + 1) % bannerCount
    //    }
    //}


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        width = measuredWidth.toFloat()
        height = measuredHeight.toFloat()
        darkWidth = width / bannerCount.toFloat()
    }

    override fun onDraw(canvas: Canvas) {
        //super.onDraw(canvas)
        paint.color = normalColor
        canvas.drawRect(0f, 0f, width, height, paint)
        paint.color = selectColor
        canvas.drawRect(darkWidth * bannerIndex, 0f, darkWidth * (bannerIndex + 1), height, paint)
    }
}